#pragma once
#include <lostlbase.h>
#include "System.h"

namespace lo {

	class TrayApp
	{
	public:
		static const	int WM_ICON_NOTIFY = WM_APP+10;
		static const	int WM_ISALIVE = WM_APP+100;		// Check if application thread is alive.
		static const	int WM_QUITFROMREMOTE = WM_APP+101;	// Quit message from another remote version of this application.
		static const	int MAX_ICONS = 2;
		enum CallbackType { Command, LeftMouseClick, RightMouseClick, MouseHover };
		typedef bool (__cdecl *Callback)( CallbackType callbackType, UINT id, LPARAM lParam );
		UINT		AddIcon(HICON hicon, HMENU leftMenu = 0, HMENU rightMenu = 0);
		HWND		GetContainerWindow()	{ return hwnd; }
		void		HideIcon( UINT id );
		void		Info( UINT id, const char *title, const char *message, UINT milliseconds, DWORD flags );
		bool		OnlyOne();	/*!< Makes sure only one valid version of this application is running. */
		void		Tooltip( UINT id, const char *message );
		int			Run(Callback callback);
		void		SetIcon(UINT id, HICON hicon, HMENU leftMenu = 0, HMENU rightMenu = 0);
		void		ShowIcon( UINT id );
		TrayApp(HINSTANCE hinstance);
		~TrayApp(void);
	private:
		HWND		CreateContainerWindow();
		void		PrepareStructure(UINT id, NOTIFYICONDATAW& notifyIconData);
		void		TrackMenu(HMENU hMenu);
		static		LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
		static		BOOL CALLBACK OnlyOneEnumWindowsProc( __in HWND hwnd, __in  LPARAM lParam );
		Callback	callback;
		UINT		currentId;
		HINSTANCE	hinstance;
		HWND		hwnd;	// Container Window
		HMENU		leftMenu[MAX_ICONS];
		bool		onlyOne;
		HMENU		rightMenu[MAX_ICONS];
		static		const	wchar_t windowClassBase[];
		static		wchar_t	windowClass[100];
		static		int		windowClassLength;
	};

}
